import { boot } from 'quasar/wrappers';
import { openDB } from 'idb';

import 'viewerjs/dist/viewer.css';
import VueViewer from 'v-viewer';

import DataVVue3 from '@kjgl77/datav-vue3';

import resizeDirective from './resizeDirective';
// "async" is optional;
// more info on params: https://v2.quasar.dev/quasar-cli/boot-files
export default boot(async ({ app, router }) => {
  // something to do
  const idb = await openDB('mdydb', 1, {
    upgrade (db) {
      db.createObjectStore('config');
    }
  });
  app.config.globalProperties.$idb = idb;

  app.use(VueViewer, {
    name: 'viewer',
    zIndex: 999,
    url: 'data-src'
  });

  app.use(DataVVue3);
  app.directive('resize', resizeDirective);

  // 取/设 自定义配置
  app.config.globalProperties.$storage = {
    get (key, pub = false) {
      return new Promise((resolve) => {
        app.config.globalProperties.$axios.get('/c/system/getStorage', { params: { key, pub } }).then(({ data }) => {
          resolve(data);
        });
      });
    },
    set (key, value, pub = false) {
      return new Promise((resolve) => {
        app.config.globalProperties.$axios.post('/c/system/setStorage', { key, value, pub }).then(({ data }) => {
          resolve(data);
        });
      });
    }
  }
})
